package com.lanou.tipsy.adapter.rank;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.entity.rank.rankInfor.SongListEntity;

import java.util.ArrayList;

/**
 * Created by wangshuai on 15/10/27.
 * 排行榜具体界面适配器
 */
public class RankInfoListAdapter extends BaseAdapter {


    //承接上下文
    private Context mContext;
    //接收数据类型
    private ArrayList<SongListEntity> datas;
    private SongListEntity entity;
    private SparseArray<String> ids;

    //构造方法
    public RankInfoListAdapter(Context mContext, ArrayList<SongListEntity> datas) {
        this.mContext = mContext;
        this.datas = datas;
        Log.i("test", "RankInfoListAdapter->datas.size():" + datas.size());
        ids = new SparseArray<>();
        getMusicSongId();
    }

    public SparseArray<String> getIds() {
        return ids;
    }

    //从适配器将热门歌曲推荐的歌曲的songId集合传送出去
    public void getMusicSongId() {
        for (int i = 0; i < datas.size(); i++) {
            final int index = i;
            entity = datas.get(i);
            String songId = entity.getSong_id();
            //songIds.add(songId);
            ids.put(index, songId);
        }
    }

    /**
     * 行数
     *
     * @return
     */
    @Override
    public int getCount() {
        if (datas != null && datas.size() > 0)
            return datas.size();
        else
            return 0;
    }

    @Override
    public SongListEntity getItem(int position) {

        if (datas != null && datas.size() > 0)
            return datas.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 内容适配
     *
     * @param position    当前地址
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_rank_info, null);
            //初始化
            holder = new ViewHolder();
            //绑定布局文件
            holder.songListIV = (SimpleDraweeView) convertView.findViewById(R.id.songListIV);
            holder.musicTitle = (TextView) convertView.findViewById(R.id.musicTitle);
            holder.musicAuthor = (TextView) convertView.findViewById(R.id.musicAuthor);
            holder.rankNumTv = (TextView) convertView.findViewById(R.id.rankNumTv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //获取当前位置
        SongListEntity songListResult = getItem(position);
        if (songListResult != null) {

            int p = position + 1;//当前歌曲的榜单名次
            Uri uri = Uri.parse(songListResult.getPic_small());
            holder.songListIV.setImageURI(uri);
            holder.musicTitle.setText(songListResult.getTitle());//添加数据
            holder.musicAuthor.setText(songListResult.getAuthor());
            if (p < 10) {
                String s = "0" + p;
                holder.rankNumTv.setText(s + "");   //歌曲榜单名次
            } else
                holder.rankNumTv.setText(p + "");   //歌曲榜单名次
        }
        //不同position设置不同红旗标志
        if (position == 0) {
            //给控件设置背景标志
            holder.rankNumTv.setBackgroundResource(R.mipmap.img_king_mask01);
        }
        if (position == 1) {
            holder.rankNumTv.setBackgroundResource(R.mipmap.img_king_mask02);
        }
        if (position == 2) {
            holder.rankNumTv.setBackgroundResource(R.mipmap.img_king_mask03);
        }


        return convertView;
    }

    //初始化
    private class ViewHolder {
        private SimpleDraweeView songListIV; // 音乐图片
        private TextView musicTitle;  //歌名
        private TextView musicAuthor;//作者
        private TextView rankNumTv;   //排行名次

    }
}